<html>
<head>
    <script src="../../lib/OpenLayers.js"></script>
    <script type="text/javascript">

    function test_initialize(t) {
        t.plan(2);
        var control = new OpenLayers.Control.DrawFeature("foo", function() {});
        t.ok(control instanceof OpenLayers.Control.DrawFeature,
             "constructor returns an instance");
        t.ok(OpenLayers.Util.indexOf(control.EVENT_TYPES, "featureadded") > -1,
             "featureadded event in EVENT_TYPES");
    }
    
    function test_multi(t) {
        t.plan(4);

        var layer = new OpenLayers.Layer.Vector();
        var control;
        
        // multi false by default
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon
        );
        t.ok(!control.multi, "control.multi false by default");
        t.ok(!control.handler.multi, "handler.multi false by default");
        
        // set on handler
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon, {multi: true}
        );
        t.ok(control.handler.multi, "handler.multi set from control options");
        
        // respect handlerOptions
        control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Polygon,
            {multi: true, handlerOptions: {multi: false}}
        );
        t.ok(!control.handler.multi, "handlerOptions.multi respected");

    }
    
    function test_drawFeature(t) {
        t.plan(3);
        var layer = new OpenLayers.Layer.Vector();
        var control = new OpenLayers.Control.DrawFeature(layer, function() {});
        var geom = {};
        
        layer.addFeatures = function(features) {
            t.ok(features[0].geometry == geom, "layer.addFeatures called");
            t.eq(features[0].state, OpenLayers.State.INSERT, "layer state set");
        };
        function handlefeatureadded(event) {
            t.ok(event.feature.geometry == geom, "featureadded triggered");
        }
        control.events.on({"featureadded": handlefeatureadded});
        control.drawFeature(geom);
        control.events.un({"featureadded": handlefeatureadded});
        
    }
    
    function test_sketch_events(t) {
        t.plan(6);
        var map = new OpenLayers.Map("map", {
            resolutions: [1]
        });
        var layer = new OpenLayers.Layer.Vector("foo", {
            maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
            isBaseLayer: true
        });
        var control = new OpenLayers.Control.DrawFeature(
            layer, OpenLayers.Handler.Point
        );
        map.addLayer(layer);
        map.addControl(control);
        map.zoomToMaxExtent();
        control.activate();
        
        var log = {};
        layer.events.on({
            sketchstarted: function(event) {
                log.event = event;
            },
            sketchmodified: function(event) {
                log.event = event;
            },
            sketchcomplete: function(event) {
                log.event = event;
            }
        });
        
        // mock up draw/modify of a point
        map.events.triggerEvent("mousedown", {xy: new OpenLayers.Pixel(0, 0)});
        t.eq(log.event.type, "sketchstarted", "[mousedown] sketchstarted triggered");
        t.geom_eq(log.event.vertex, new OpenLayers.Geometry.Point(-200, 125), "[mousedown] correct vertex");
        map.events.triggerEvent("mousemove", {xy: new OpenLayers.Pixel(10, 10)});
        t.eq(log.event.type, "sketchmodified", "[mousemove] sketchmodified triggered");
        t.geom_eq(log.event.vertex, new OpenLayers.Geometry.Point(-190, 115), "[mousemove] correct vertex");
        map.events.triggerEvent("mouseup", {xy: new OpenLayers.Pixel(10, 10)});
        t.eq(log.event.type, "sketchcomplete", "[mouseup] sketchcomplete triggered");
        t.geom_eq(log.event.feature.geometry, new OpenLayers.Geometry.Point(-190, 115), "[mouseup] correct geometry");
        
        map.destroy();
    }

    </script>
</head>
<body>
    <div id="map" style="width: 400px; height: 250px;"/>
</body>
</html>
